import moment from "moment/moment";
import { NativeModules } from 'react-native'

const trackNative = NativeModules.YuanXinTrack

/**
 * Created by yuzhang on 2018/4/26.
 */

export function forIn (object, fn) {
  if (object === null) { return object }
  let objectKeys = Object.keys(object)
  for (let key of objectKeys) {
    if (object[key]) {
      fn(object[key], key)
    }
  }
  return object
}

export function isFunc (func) {
  return typeof func === 'function'
}

export function guid () {
  if (isFunc(trackNative.getGuid)) { // 用原生模块生产 uuid
    // return trackNative.getGuid()
  }
  function s4 () {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1)
  }

  return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4()
}

export function testUniqueID (fn, testObjArray, testCount) {
  let obj = {}
  let count = testCount
  let dump = 0
  while (count--) {
    let key = fn()
    for (let i = 0; i < testObjArray.length; i++) {
      let testObj = testObjArray[i]
      console.log('待测试对象' + i + '中第一个key:' + Object.keys(testObj)[0])
      dump += testObj.dump
      if (testObj[key]) {
        dump++
      }
    }
    obj[key] = 1
  }
  obj.dump = testCount - Object.keys(obj).length + dump
  return obj
}
// 测试 guid 生成一亿个id中的重复数，结果为0
// 很耗时，大约 15 分钟
// 测试机器：Mac mini (Late 2012)
// 处理器：2.5 GHz Intel Core i5
// 内存：16 GB 1600 MHz DDR3
// 启动磁盘：SSD256G
export function testForUniqueID () {
  const testCount = 10000 * 100 // 单次测试的个数
  let count = 100 // 循环100次
  let obj = {dump: 0}
  let objs = new Array()
  for (let i = 0; i < count; i++) {
    obj = testUniqueID(guid, objs, testCount)
    objs.concat(obj)
    console.log('测试生成' + testCount * (i + 1) + '个唯一ID时的重复数:' + obj.dump)
  }
}

// 测试 guid 生成一亿个id中的重复数，结果为0
// testForUniqueID()

// 将时间格式化成特定的字符串形式
export function formatDate (now) {
  let date = moment(now)

  return date.format('YYYY-MM-DDTHH:mm:ss.SSSZ')
}

export function wrapObject (obj, key) {
  let extra = {}
  if (typeof (obj) === 'object' && Object.prototype.toString.call(obj).toLowerCase() === '[object object]' && !obj.length) {
    extra = obj
  } else if (obj !== undefined) {
    if (typeof key !== 'string') {
      key = 'extraData'
    }
    // extra = {key: obj}
    extra[key] = obj
  }
  return extra
}

// 验证duration超限时间，防止记录出错
export function verifyDuration (millisecond) {
  if (!millisecond) {return millisecond}
  if (millisecond < 0) {return 0}
  if (millisecond > 24 * 60 * 60 * 1000) { //如果大于一天
    return 60 * 60 * 1000 // 返回一个小时
  }
  return millisecond
}


Date.prototype.Format = function(fmt)
{ //author: meizz
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}
